#include "gatherDataHandler.h"
#include "gather_task.h"
GatherTargetData targetData;
extern int heartState;

// 清洗数据
u8 cleanData(const GatherData* source)
{
    u8 times = 0;
    if (source == NULL)
    {
        printf("error: source data is null!\r\n");
        return 1;
    }
    if (source -> temp > 200)
    {
        times++;
        if (times > 10)
        {
            heartState = 0;
            return 0; 
        }
        return 1;
    }
    else
    {
        // 重置状态
        times = 0;
        heartState = 1;
        return 0;
    }
}
// 处理数据
void processData(const GatherData* source)
{
    printf("process starting...\r\n");
    // 温湿度
    targetData.temp = source -> temp;
    targetData.humi = source -> humi;
    printf("read data: temp = %d humi = %d\r\n", targetData.temp, targetData.humi);
    // 烟雾
    targetData.smoke = source -> smoke / 40;
    // 光敏
    targetData.sunshine = 100 - source -> smoke / 40;
    // 水滴
    targetData.raidrop = source -> raidrop;
}
// 处理数据调用
void dataHandle(const GatherData* source)
{
    if (cleanData(source)) return;
    if (heartState == 0) return;
    processData(source);
}